package org.example;

import org.openjdk.jol.info.ClassLayout;

import java.util.ArrayList;
import java.util.List;

public class Main9 {

    /**
     * 批量重偏向
     */
    private static Thread t1,t2;
    public static void main(String[] args) throws Exception {


        List<A> list = new ArrayList<>();
        //生成40个A的实例
        for (int i = 0; i < 40; i++) {
            list.add(new A());
        }

         t1 = new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                A a = list.get(i);
                synchronized (a) {
                    if (i == 1 || i == 19) {
                        System.out.println("t1 lock " + i);
                        //打印对象头
                        System.out.println(ClassLayout.parseInstance(a).toPrintable());//偏向锁
                    }
                }
            }
        },"线程1");
        t1.start();
        t1.join();//通过join是t1结束后再启动t2,避免竞争
         t2 = new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                A a = list.get(i);
                synchronized (a) {
                    if (i == 1 || i == 19) {//i<19的时候轻量锁， i>=19的时候是偏向锁
                        System.out.println("t2 lock " + i);
                        System.out.println(ClassLayout.parseInstance(a).toPrintable());
                    }
                }
            }
        },"线程2");
        t2.start();
        t2.join();

        /**
         * t1 lock 1
         * # WARNING: Unable to get Instrumentation. Dynamic Attach failed: null. You may add this JAR as -javaagent manually.
         * WARNING: An illegal reflective access operation has occurred
         * WARNING: Illegal reflective access by org.openjdk.jol.vm.sa.ServiceabilityAgentSupport (file:/C:/Users/CAIKE3/.m2/repository/org/openjdk/jol/jol-core/0.8/jol-core-0.8.jar) to field sun.management.RuntimeImpl.jvm
         * WARNING: Please consider reporting this to the maintainers of org.openjdk.jol.vm.sa.ServiceabilityAgentSupport
         * WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
         * WARNING: All illegal access operations will be denied in a future release
         * org.example.A object internals:
         *  OFFSET  SIZE      TYPE DESCRIPTION                               VALUE
         *       0     4           (object header)                           05 50 b2 99 (00000101 01010000 10110010 10011001) (-1716367355)
         *       4     4           (object header)                           f7 01 00 00 (11110111 00000001 00000000 00000000) (503)
         *       8     4           (object header)                           48 72 06 00 (01001000 01110010 00000110 00000000) (422472)
         *      12     1   boolean A.flag                                    false
         *      13     3           (loss due to the next object alignment)
         * Instance size: 16 bytes
         * Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
         *
         * t1 lock 19
         * org.example.A object internals:
         *  OFFSET  SIZE      TYPE DESCRIPTION                               VALUE
         *       0     4           (object header)                           05 50 b2 99 (00000101 01010000 10110010 10011001) (-1716367355)
         *       4     4           (object header)                           f7 01 00 00 (11110111 00000001 00000000 00000000) (503)
         *       8     4           (object header)                           48 72 06 00 (01001000 01110010 00000110 00000000) (422472)
         *      12     1   boolean A.flag                                    false
         *      13     3           (loss due to the next object alignment)
         * Instance size: 16 bytes
         * Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
         *
         * t2 lock 1
         * org.example.A object internals:
         *  OFFSET  SIZE      TYPE DESCRIPTION                               VALUE
         *       0     4           (object header)                           05 50 b2 99 (00000101 01010000 10110010 10011001) (-1716367355)
         *       4     4           (object header)                           f7 01 00 00 (11110111 00000001 00000000 00000000) (503)
         *       8     4           (object header)                           48 72 06 00 (01001000 01110010 00000110 00000000) (422472)
         *      12     1   boolean A.flag                                    false
         *      13     3           (loss due to the next object alignment)
         * Instance size: 16 bytes
         * Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
         *
         * t2 lock 19
         * org.example.A object internals:
         *  OFFSET  SIZE      TYPE DESCRIPTION                               VALUE
         *       0     4           (object header)                           05 50 b2 99 (00000101 01010000 10110010 10011001) (-1716367355)
         *       4     4           (object header)                           f7 01 00 00 (11110111 00000001 00000000 00000000) (503)
         *       8     4           (object header)                           48 72 06 00 (01001000 01110010 00000110 00000000) (422472)
         *      12     1   boolean A.flag                                    false
         *      13     3           (loss due to the next object alignment)
         * Instance size: 16 bytes
         * Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
         */
    }

}

